
const {Menu,app,shell} = require('electron')
const appToast = require('./app-toast')
const DataStore = require('./app-store')
const dataStore = new DataStore()




//根据id匹配
function myGetMenuItemById(id, myMenu) {
    const items = myMenu.items
    let found = items.find(item => item.id === id) || null
    for (let i = 0, length = items.length; !found && i < length; i++) {
        if (items[i].submenu) {
            found = myGetMenuItemById(id, items[i].submenu)
        }
    }
    return found
}

/**
 * 关闭除ID外的其他checked
 */
//给语速设置子菜单配置 id 每一个小的选项对应一个字符串id  spdx
let spdIds = ['spd1', 'spd2', 'spd3', 'spd4', 'spd5', 'spd6', 'spd7', 'spd8', 'spd9'];
let apiIds = ['sogou', 'youdao'];
function closeMenuChecked(id, menu, subLabels) {
    for (let myId of subLabels) {
        if (id !== myId) {
            myGetMenuItemById(myId, menu).checked = false
        }
    }
}

exports.buildContextMenu = function buildContextMenu(tray) {
	// 菜单栏引用
    let menu;
    //语速设置子菜单
    let spdSubMenu = new Array(9);
    let localSpd = dataStore.getSpd();
    for (let i = 0; i < 9; i++) {
        let spd = i + 1;
        spdSubMenu[i] = {
            id: spdIds[i],
            label: '语速' + spd,
            type: 'checkbox',
            checked: localSpd === spd,
            click: function (menuItem, browserWindow, event) {
                menuItem.checked = true;
                dataStore.setSpd(spd);
                closeMenuChecked(spdIds[i], menu, spdIds);
            }
        };
    }

    //TTS api 子菜单
    let localTTSApi = dataStore.getTTSApi();
    let apiSubMenu = [
        {
            id: apiIds[0],
            label: '搜狗(可变速1快9慢)',
            type: 'checkbox',
            checked: localTTSApi.indexOf(apiIds[0]) > 0,
            click: function (menuItem, browserWindow, event) {
                menuItem.checked = true;
                dataStore.setTTSApi("https://fanyi.sogou.com/reventondc/synthesis?text=__TEXT__&speed=__SPD__&lang=zh-CHS&from=translateweb&speaker=6");
                closeMenuChecked(apiIds[0],menu,apiIds);
            }
        },
        {
            id: apiIds[1],
            label: '有道(不可调速)',
            type: 'checkbox',
            checked: localTTSApi.indexOf(apiIds[1]) > 0,
            click: function (menuItem, browserWindow, event) {
                menuItem.checked = true;
                dataStore.setTTSApi("https://tts.youdao.com/fanyivoice?word=__TEXT__&le=zh&keyfrom=speaker-target");
                closeMenuChecked(apiIds[1], menu, apiIds);
            }
        }
    ];


    const template = [
        {
            label: '语速设置',
            submenu:spdSubMenu
        },
        {
            label: 'Api切换',
            submenu:apiSubMenu
        },
        // {
        //     label: 'Github',
        //     click: () => {
        //         shell.openExternal(require('./app-constant').github).catch()
        //     }
        // },
        {
            label: 'Gitee',
            click: () => {
                shell.openExternal(require('./app-constant').gitee).catch()
            }
        },
        {
            label: '反馈',
            click: () => {
                shell.openExternal(require('./app-constant').issues).catch()
            }
        },
		{
            label: '退出程序',
            click: () => {
                tray.destroy()
                app.quit()
            }
        }
    ];
	menu = Menu.buildFromTemplate(template)
    return menu
}